﻿using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Linq;
using System.Text;

namespace Eventbrite.LinqProvider.Translators
{
    public  class OrderByTranslator:  Helpers.ExpressionVisitor
    {
       string _columnName;
      // Eventbrite service allows to order results using only these columns
        Dictionary<string, object> _nameLookup = new Dictionary<string, object>() { { "date", null }, { "city", null }, { "name", null }, { "id", null } };
        public string OrderByClouse
        {
            get
            {
                 if(_columnName == "organizer")
                   _columnName = "name";
                return _columnName.ToLower();

            }
        }

        protected override Expression VisitMemberAccess(MemberExpression m)
        {
            var name =  m.Member.Name.ToLower();
            if(_nameLookup.ContainsKey(name))
                this._columnName =name ;
            else
                throw new ArgumentException(string.Format("{0} member is not supported in OrderBy", name));
          
            return m;
        }
      
        internal void Translate(Expression exp)
        {
            Visit(exp);
        }
   }
}
